package poc;

import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.Word;
import org.fit.pdfdom.PDFDomTree;

import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by LiuLin on 2018/10/25.
 */
public class PdfToHtml {
    public static void main(String[] args){
        String pdfPath = "C:\\Users\\LL\\Desktop\\600740_20181016_1.pdf";
        String htmlPath = "C:\\Users\\LL\\Desktop\\600740_20181016_1.html";
        String bilingualPdfPath = "D:\\MetaSota\\翻译对齐结果\\11.14对齐文件及结果\\Hungary  OC (CN).pdf";
        try{
            generateHTMLFromPDF(pdfPath,htmlPath);
//            pdfToTxt(pdfPath,null);
            //bilingualPdfToTxt(bilingualPdfPath);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void generateHTMLFromPDF(String pdfPath,String htmlPath) throws IOException, ParserConfigurationException {
        PDDocument pdf = PDDocument.load(new File(pdfPath));
        if(htmlPath == null || htmlPath.length() == 0){
            htmlPath = pdfPath.replace(".pdf",".html");
        }
        Writer output = new PrintWriter(htmlPath, "utf-8");
        new PDFDomTree().writeText(pdf, output);
        output.close();
    }

    public static void pdfToTxt(String pdfPath,String txtPath){
        try{
            if(txtPath == null || txtPath.length() == 0){
                txtPath = pdfPath.replace(".pdf","txt");
            }
            PDDocument document = PDDocument.load(new File(pdfPath));

            PDFTextStripper pts = new PDFTextStripper();
            int endPage = document.getNumberOfPages();
            System.out.println("Total Page: " + endPage);
            pts.setStartPage(1);
            pts.setEndPage(endPage);
            pts.setSortByPosition(false);
            pts.setLineSeparator(" ");
            //设置段落标志方便分段
            pts.setParagraphStart("paragraphStart---:");
            pts.setParagraphEnd("\n");

            //设置页签
            PDOutlineItem bookmarkStarterPdOutlineItem = new PDOutlineItem();
            bookmarkStarterPdOutlineItem.setTitle("\\$bookmarkStart\\$");
            pts.setStartBookmark(bookmarkStarterPdOutlineItem);
            PDOutlineItem bookmarkEndPdOutlineItem = new PDOutlineItem();
            bookmarkEndPdOutlineItem.setTitle("\\$bookmarkEnd\\$");
            pts.setEndBookmark(bookmarkEndPdOutlineItem);
            System.out.println(pts.getText(document));
            PrintWriter writer = new PrintWriter(new FileOutputStream(txtPath));
            pts.writeText(document,writer);
            writer.flush();
            writer.close();
            if (null != document){
                document.close();
            }
            System.out.println("Get PDF Content ...");
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static void bilingualPdfToTxt(String pdfPath){
        try{
            PDDocument document = PDDocument.load(new File(pdfPath));
            PDFTextStripper pts = new PDFTextStripper();
            int endPage = document.getNumberOfPages();
            System.out.println("Total Page: " + endPage);
            pts.setStartPage(1);
            pts.setEndPage(24);
            pts.setSortByPosition(false);
            pts.setLineSeparator(" ");
            //设置段落标志方便分段
            //pts.setParagraphStart("paragraphStart---:");
            pts.setParagraphEnd("\n");

            //设置页签
            PDOutlineItem bookmarkStarterPdOutlineItem = new PDOutlineItem();
            bookmarkStarterPdOutlineItem.setTitle("$bookmarkStart$");
            pts.setStartBookmark(bookmarkStarterPdOutlineItem);
            PDOutlineItem bookmarkEndPdOutlineItem = new PDOutlineItem();
            bookmarkEndPdOutlineItem.setTitle("$bookmarkEnd$");
            pts.setEndBookmark(bookmarkEndPdOutlineItem);

            //存储中文段落
            List<String> zhParagraphList = new ArrayList<>();
            String zhParagraph = "";
            //存储英文段落
            List<String> enParagraphList = new ArrayList<>();
            String enParagraph = "";

            //前一句语言
            String preSentenceLan = "ot";

            String content = pts.getText(document);
            String[] paragraphs = content.split("\\n");

            for(String paragraph:paragraphs){

                String dealWithedParagraph = DocxToTxt.dealWithDocxParagraph(paragraph.trim());
                if(dealWithedParagraph.length() == 0 || PdfTransformUtil.needIgnnoredStrings.contains(dealWithedParagraph)){
                    continue;
                }

                String curSentenceLan = DocxToTxt.getSentenceLan(dealWithedParagraph);

                //字符长度
                int zhParagraphLen = zhParagraph.length();
                int enParagraphLen = enParagraph.length();

                //分词后词组长度
                int zhParagraphWordsLen = WordSegmenter.segWithStopWords(zhParagraph).size();
                int enParagraphWordsLen = WordSegmenter.segWithStopWords(enParagraph).size();

                if(zhParagraphWordsLen > 0 && enParagraphWordsLen > 0 && !preSentenceLan.equals(curSentenceLan)){
                    if(!(zhParagraphLen > 3*enParagraphLen || zhParagraphLen < enParagraphLen/3
                            || enParagraphLen > 3*zhParagraphLen || enParagraphLen < zhParagraphLen/3)){
                        //字符长度符合条件直接加
                        enParagraphList.add(enParagraph);
                        enParagraph = "";
                        zhParagraphList.add(zhParagraph);
                        zhParagraph = "";
                    }else{
                        //否则比较单词长度
                        if(!(zhParagraphWordsLen > 3*enParagraphWordsLen || zhParagraphWordsLen < enParagraphWordsLen/3
                                || enParagraphWordsLen > 3*zhParagraphWordsLen || enParagraphWordsLen < zhParagraphWordsLen/3)){
                            //单词长度符合条件
                            enParagraphList.add(enParagraph);
                            enParagraph = "";
                            zhParagraphList.add(zhParagraph);
                            zhParagraph = "";
                        }else{
                            //否则都舍弃
                            enParagraph = "";
                            zhParagraph = "";
                        }
                    }
                }

                if(curSentenceLan.equals("zh")){
                    zhParagraph += dealWithedParagraph;
                    preSentenceLan = "zh";
                }else if(curSentenceLan.equals("en")){
                    enParagraph += dealWithedParagraph + " ";
                    preSentenceLan = "en";
                }

//                String[] sentences = dealWithedParagraph.split("\\t");
//                for(String sentence:sentences){
//                    String dealWithedSentence = DocxToTxt.dealWithDocxParagraph(sentence.trim());
//                    if(dealWithedSentence.length() > 0){
//
//                    }
//                }

            }

            for(int i = 0;i<zhParagraphList.size() && i<enParagraphList.size();i++){
                System.out.println("中文："+zhParagraphList.get(i));
                System.out.println("英文："+enParagraphList.get(i));
            }

            String writeZhFileContent = "";
            String writeEnFileContent = "";

            for(int i = 0; i < zhParagraphList.size(); i++){
                writeZhFileContent += "paragraph" + (i+1) + ":  " + zhParagraphList.get(i) + "\n";
                //writeZhFileContent += zhParagraphList.get(i) + "\n";
            }

            for(int i = 0; i < enParagraphList.size(); i++){
                writeEnFileContent += "paragraph" + (i+1) + ":  " + enParagraphList.get(i) + "\n";
                //writeEnFileContent += enParagraphList.get(i) + "\n";
            }

            DocxToTxt.writeTxtFile(pdfPath.replace(".pdf","_zh.txt"),writeZhFileContent);
            DocxToTxt.writeTxtFile(pdfPath.replace(".pdf","_en.txt"),writeEnFileContent);

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static boolean endwithPunctuation(String sentence,String language){
        if(language.equals("en")){
            return true;
        }
        if(sentence.endsWith("，") || sentence.endsWith("。")){
            return true;
        }
        return false;
    }

    public static void testWordSegmenter(){
            String zhParagraph = "本期债券发行金额 ： 人民币10亿元";
            String enParagraph = "Amount of this Series of the Bonds to be Issued ： RMB1 billion";
            List<Word> zhWords = WordSegmenter.segWithStopWords(zhParagraph);
            List<Word> enWords = WordSegmenter.segWithStopWords(enParagraph);
            System.out.println(zhWords);
            System.out.println(enWords);
            int zhParagraphLen = zhWords.size();
            int enParagraphLen = enWords.size();
            System.out.println("zhParagraphLen ==== "+zhParagraphLen+"  enParagraphLen === "+enParagraphLen);
    }

}
